var app = new Vue({
    el: '#app',
    data: {
        host: host,
        username: '',
        mobile: '',
        email: '',
        wallet_income:'',
        nick_name:'',
        nick_error:'',
        histories:'',
        email_active: false,
        set_email: false,

        email_error: false,
        send_email_btn_disabled: false,
        send_email_tip: '',


    },
    mounted: function () {
        // 获取个人信息
        this.get_person();

        let u_json = $cookies.get('username')   //使用第三方库获取cookie
        let u_str = JSON.parse(u_json)    //将数据进行json反序列化
        this.username = eval(u_str)     //将unicode编码转为中文
    
            
        // this.get_person_info()
    
        this.get_history()


    },
    methods: {
        get_history:function(){
            // 添加下列代码, 发送请求, 获取用户的浏览记录信息:
           axios.get(this.host + '/browse_histories/', {
                   responseType: 'json',
                   withCredentials:true,
               })
               .then(response => {
                   this.histories = response.data.skus;
                   for(var i=0; i<this.histories.length; i++){
                     this.histories[i].url='/detail.html?sku='+this.histories[i].id ;
                   }
               })
               .catch(error => {
                   console.log(error)
               });
       },
        get_person: function () {
            var url = this.host + '/userinfo/'
            // console.log(url)
            axios.get(url, {
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    // console.log(response.data)
                    if (response.data.code == 400) {

                        //next=是如果未登录要跳转的页面
                        location.href = 'login.html?next='+location.href;
                        return
                    }
                    this.username = response.data.context.username;
                    this.nick_name = response.data.context.nick_name;
                    this.mobile = response.data.context.mobile;
                    this.wallet_income = response.data.context.wallet_income;
                    this.email = response.data.context.email;
                    this.email_active = response.data.context.email_active;


                })

                .catch(error => {
                    this.set_email = false;
                    location.href = 'login.html'
                    console.log(error.response)
                })
        },


        logoutfunc: function () {
            var url = this.host + '/logout/'
            axios.delete(url,{
                responseType: 'json',
                withCredentials:true,
            })
                .then(response =>{
                    // console.log(response.data)
                    location.href='login.html'

                })
                .catch(error =>{
                    console.log(error.response)
                })
        },




        amend_name:function () {
          var url = this.host + '/nickname/';
          axios.put(url,{
              nick_name:this.nick_name,
          },{
              responseType: 'json',
              withCredentials:true,
          })
              .then( resp =>{
                    if(resp.data.code==200){
                        this.nick_error='';
                    }else {
                        this.nick_error='修改失败';
                    }
              })
              .catch( error =>{
                  console.log(error.response)
              })

        },

        //校验邮箱格式
        save_email: function () {
            var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if (re.test(this.email)) {
                this.email_error = false;
            } else {
                this.email_error = true;
                return
            }
            //前端请求数据
            var url = this.host + '/email/';
            axios.put(url, {
                email: this.email,
            }, {
                responseType: 'json',
                withCredentials: true,

            })
                .then(response => {

                    // console.log(response.data)
                    this.set_email = false;
                    this.send_email_btn_disabled = true;
                    this.send_email_tip = '已发送邮件'

                })
                .catch(error => {
                    console.log(error.response)
                })
        }

    }

})